package com.example.adapter;

import java.util.List;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.item.ItemSMS;
import com.example.smsbox.R;

public class SmsAdapter extends ArrayAdapter<ItemSMS> {

	private LayoutInflater inflater;

	public SmsAdapter(Context context, int textViewResourceId,
			List<ItemSMS> objects) {
		super(context, textViewResourceId, objects);
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemSMS item = this.getItem(position);
		ViewHolder holder = new ViewHolder();

		if (null == convertView) {
			convertView = inflater.inflate(R.layout.row_chat, null);
			holder.txtNoidung = (TextView) convertView
					.findViewById(R.id.txtNoiDung);
			// holder.txtThoigian = (TextView) convertView
			// .findViewById(R.id.txtThoiGian);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		RelativeLayout relativeSMS = (RelativeLayout) convertView
				.findViewById(R.id.relativeSMS);
		RelativeLayout relativeMe = (RelativeLayout) convertView
				.findViewById(R.id.relativeMe);
		RelativeLayout relativeFriend = (RelativeLayout) convertView
				.findViewById(R.id.relativeFriend);

		if (item.getGuinhan() == 0) {
			relativeSMS.setGravity(Gravity.LEFT);
			holder.txtNoidung.setGravity(Gravity.LEFT);
			relativeFriend.setVisibility(View.INVISIBLE);
			relativeMe.setVisibility(View.VISIBLE);
			

		} else if (item.getGuinhan() == 1) {
			relativeSMS.setGravity(Gravity.RIGHT);
			holder.txtNoidung.setGravity(Gravity.RIGHT);
			relativeMe.setVisibility(View.INVISIBLE);
			relativeFriend.setVisibility(View.VISIBLE);
		}

		holder.txtNoidung.setText(item.getMessaging());
		// holder.txtThoigian.setText(item.getThoigian());

		return convertView;
	}

	public class ViewHolder {
		private TextView txtNoidung;
		// private TextView txtThoigian;
	}

}
